package com.ttai.untils;

import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * @program: taisecurity
 * @description: SRP工具
 * @author: Jingxi Zhu
 * @create: 2018-11-20 11:07
 * @Copyright © 2018 Jingxi Zhu. All rights reserved
 **/
public class SRPUtil {
    public static final int HASH_LEN = 20;
    public static final char[] TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./".toCharArray();
    public static Random RNG = new SecureRandom();
    private static MessageDigest SHADigest;

    public static MessageDigest newDigest() {
        MessageDigest var0 = null;

        try {
            var0 = (MessageDigest)SHADigest.clone();
        } catch (CloneNotSupportedException var1) {
            System.err.println("Fatal error: Unable to clone SHA-1 digest");
            System.exit(1);
        }

        return var0;
    }

    public static String tob64(byte[] var0) {
        boolean var1 = false;
        int var2 = var0.length;
        int var3 = var2 % 3;
        byte var5 = 0;
        byte var6 = 0;
        byte var7 = 0;
        StringBuffer var8 = new StringBuffer();
        switch(var3) {
            case 1:
                var7 = var0[0];
                break;
            case 2:
                var6 = var0[0];
                var7 = var0[1];
        }

        while(true) {
            int var4 = (var5 & 252) >>> 2;
            if (var1 || var4 != 0) {
                var8.append(TABLE[var4]);
                var1 = true;
            }

            var4 = (var5 & 3) << 4 | (var6 & 240) >>> 4;
            if (var1 || var4 != 0) {
                var8.append(TABLE[var4]);
                var1 = true;
            }

            var4 = (var6 & 15) << 2 | (var7 & 192) >>> 6;
            if (var1 || var4 != 0) {
                var8.append(TABLE[var4]);
                var1 = true;
            }

            var4 = var7 & 63;
            if (var1 || var4 != 0) {
                var8.append(TABLE[var4]);
                var1 = true;
            }

            if (var3 >= var2) {
                break;
            }

            try {
                var5 = var0[var3++];
                var6 = var0[var3++];
                var7 = var0[var3++];
            } catch (ArrayIndexOutOfBoundsException var9) {
                break;
            }
        }

        return var1 ? var8.toString() : "0";
    }

    public static byte[] fromb64(String var0) throws NumberFormatException {
        int var1 = var0.length();
        if (var1 == 0) {
            throw new NumberFormatException("Empty Base64 string");
        } else {
            byte[] var2 = new byte[var1 + 1];

            int var4;
            int var5;
            for(var4 = 0; var4 < var1; ++var4) {
                char var3 = var0.charAt(var4);
                try {
                    for(var5 = 0; var3 != TABLE[var5]; ++var5) {
                        ;
                    }
                } catch (Exception var9) {
                    throw new NumberFormatException("Illegal Base64 character");
                }

                var2[var4] = (byte)var5;
            }

            var4 = var1 - 1;
            var5 = var1;

            try {
                do {
                    var2[var5] = var2[var4];
                    --var4;
                    if (var4 < 0) {
                        break;
                    }

                    var2[var5] = (byte)(var2[var5] | (var2[var4] & 3) << 6);
                    --var5;
                    var2[var5] = (byte)((var2[var4] & 60) >>> 2);
                    --var4;
                    if (var4 < 0) {
                        break;
                    }

                    var2[var5] = (byte)(var2[var5] | (var2[var4] & 15) << 4);
                    --var5;
                    var2[var5] = (byte)((var2[var4] & 48) >>> 4);
                    --var4;
                    if (var4 < 0) {
                        break;
                    }

                    var2[var5] = (byte)(var2[var5] | var2[var4] << 2);
                    var2[var5 - 1] = 0;
                    --var5;
                    --var4;
                } while(var4 >= 0);
            } catch (Exception var8) {
                ;
            }

            try {
                while(var2[var5] == 0) {
                    ++var5;
                }
            } catch (Exception var7) {
                return new byte[1];
            }

            byte[] var6 = new byte[var1 - var5 + 1];
            System.arraycopy(var2, var5, var6, 0, var1 - var5 + 1);
            return var6;
        }
    }

    private static final int asciiToHex(char var0) {
        if (var0 >= 'a' && var0 <= 'f') {
            return var0 - 97 + 10;
        } else if (var0 >= 'A' && var0 <= 'F') {
            return var0 - 65 + 10;
        } else {
            return var0 >= '0' && var0 <= '9' ? var0 - 48 : 0;
        }
    }

    private static final char hexToAscii(int var0) {
        if (var0 >= 10 && var0 <= 15) {
            return (char)(65 + (var0 - 10));
        } else {
            return var0 >= 0 && var0 <= 9 ? (char)(48 + var0) : '0';
        }
    }

    public static String tohex(byte[] var0) {
        StringBuffer var1 = new StringBuffer();

        for(int var2 = 0; var2 < var0.length; ++var2) {
            var1.append(hexToAscii(var0[var2] >>> 4 & 15)).append(hexToAscii(var0[var2] & 15));
        }

        return var1.toString();
    }

    public static byte[] fromhex(String var0) {
        int var1 = var0.length();
        int var2 = (var1 + 1) / 2;
        byte[] var3 = new byte[var2];
        --var1;
        --var2;

        while(var1 > 0) {
            var3[var2] = (byte)(asciiToHex(var0.charAt(var1 - 1)) << 4 | asciiToHex(var0.charAt(var1)));
            var1 -= 2;
            --var2;
        }

        if (var1 > 0) {
            var3[0] = (byte)asciiToHex(var0.charAt(0));
        }

        return var3;
    }

    public static byte[] trim(byte[] var0) {
        if (var0.length != 0 && var0[0] == 0) {
            int var1 = var0.length;

            int var2;
            for(var2 = 1; var0[var2] == 0 && var2 < var1; ++var2) {
                ;
            }

            byte[] var3 = new byte[var1 - var2];
            System.arraycopy(var0, var2, var3, 0, var1 - var2);
            return var3;
        } else {
            return var0;
        }
    }

    public static byte[] userHash(String var0, String var1) {
        return newDigest().digest((var0 + ":" + var1).getBytes());
    }

    public static byte[] sessionKeyHash(byte[] var0) {
        int var2;
        for(var2 = 0; var2 < var0.length && var0[var2] == 0; ++var2) {
            ;
        }

        byte[] var3 = new byte[40];
        int var5 = (var0.length - var2) / 2;
        byte[] var6 = new byte[var5];

        int var1;
        for(var1 = 0; var1 < var5; ++var1) {
            var6[var1] = var0[var0.length - 2 * var1 - 1];
        }

        byte[] var4 = newDigest().digest(var6);

        for(var1 = 0; var1 < 20; ++var1) {
            var3[2 * var1] = var4[var1];
        }

        for(var1 = 0; var1 < var5; ++var1) {
            var6[var1] = var0[var0.length - 2 * var1 - 2];
        }

        var4 = newDigest().digest(var6);

        for(var1 = 0; var1 < 20; ++var1) {
            var3[2 * var1 + 1] = var4[var1];
        }

        return var3;
    }

    public static boolean matches(byte[] var0, byte[] var1) {
        int var2 = var0.length;
        if (var2 != var1.length) {
            return false;
        } else {
            do {
                --var2;
                if (var2 < 0) {
                    return true;
                }
            } while(var0[var2] == var1[var2]);

            return false;
        }
    }

    public static byte[] xor(byte[] var0, byte[] var1, int var2) {
        byte[] var3 = new byte[var2];

        for(int var4 = 0; var4 < var2; ++var4) {
            var3[var4] = (byte)(var0[var4] ^ var1[var4]);
        }

        return var3;
    }

    /**
     * 根据自定义分隔符symbol切分字符串
     */
    public static List<String> spiString(String str, String symbol){
        List<String> result = new LinkedList<>();
        String[] strs=str.split(symbol);
        for(String s : strs){
            result.add(s);
        }
        return result;
    }

    public SRPUtil() {
    }

    static {
        try {
            SHADigest = MessageDigest.getInstance("SHA-1");
        } catch (Exception var0) {
            System.err.println("Fatal error: SHA-1 digest not supported!");
            System.exit(1);
        }
    }
}
